
//var counter = 1;

function addCriteriaRow(tableID)
{
	counter = document.getElementById("criteria_counter").value;
	var i = counter-1;
	
	
	if(document.getElementById("text_value"+i) != null)
	{
	
		if(document.getElementById("text_value"+i).value=="")
		{
			message = "Please Enter Value Before Inserting Another Rule";
			alert(message);
			document.getElementById("text_value"+i).focus();
			return false;
		}
		var table  =  document.all ? document.all[tableID] : document.getElementById(tableID);
		if (document.all || document.getElementById)
		{
			var row  = table.insertRow(table.rows.length-2);
			var cell1 = row.insertCell(0);
			var cell2 = row.insertCell(1);
			var cell3 = row.insertCell(2);
			var cell4 = row.insertCell(3);
			var cell5 = row.insertCell(4);
			var cell6 = row.insertCell(5);
			var cell7 = row.insertCell(6);
			var cell8 = row.insertCell(7);
			
					
			row.setAttribute( 'id' , 'rule'+counter);
			cell1.innerHTML = '<td class="Labels" >Field</td>';
			
			var str = '<select name="combo_field[]" id="combo_field'+counter+'" class="InputComboList">';
			
			for(i=0; i<document.getElementById("combo_field0").options.length; i++) {
				
				str += "<option value=\"" + document.getElementById("combo_field0").options[i].value +"\">" +
				        document.getElementById("combo_field0").options[i].text+"</option>";
			   
			}
			str += "</select>";
			cell2.innerHTML  = str;
			cell3.innerHTML = '<td class="Labels" >Logic</td>';
			
			var logicStr = '<select name="combo_logic[]" id="combo_logic'+counter+'" class="InputComboList">';

			for(i=0; i<document.getElementById("combo_logic0").options.length; i++) {
				
				logicStr += "<option value=\"" + document.getElementById("combo_logic0").options[i].value +"\">" +
				        document.getElementById("combo_logic0").options[i].text+"</option>";
			   
			}
			logicStr += "</select>";
			cell4.innerHTML  = logicStr;
			
			cell5.innerHTML = '<td class="Labels" >Value</td>';
			cell6.innerHTML  = '<input name="text_value[]" type="text" class="InputText" id="text_value'+counter+'" >';
			
			cell7.innerHTML  = '<input type="button" name="btn_addCriteriaRow" id="'+'btn_addCriteriaRow'+counter+'" class="InputButton" value="Add" onClick="addCriteriaRow(\'table1\');" ></td>';
			cell8.innerHTML  = '<input type="button" name="'+'rule'+counter+'_btn" id="'+'rule'+counter+'_btn" class="InputButton" value="Remove" onClick="removeCriteriaRow(\'table1\',this);" ></td>';

	counter++;
	document.getElementById("criteria_counter").value= counter;	
//	document.getElementById("text_value"+counter).focus();
	

		}
	}
	
}


function removeCriteriaRow(tableID, row_obj) 
{
	
	var tab = document.getElementById(tableID);
	var id=row_obj.id;
	var rowname=id.substring(0,id.indexOf('_'));
	var rowobj = document.getElementById(rowname);
	tab.deleteRow(rowobj.rowIndex);
	counter = document.getElementById("criteria_counter").value ;
	document.getElementById("criteria_counter").value = --counter;
}
	
function loadValues(formId,comboId){
		
		//var val = document.getElementById(comboId).value;
		document.getElementById(formId).action = "../../view/Rule/ViewRule.php?param="+document.getElementById('combo_rule_meta').value;
		document.getElementById(formId).submit();
		
		//document.getElementById(form1).action="../../business/RetrieveRules.php?param=viewRule";
}	
	
function submitNow(formId,oper){
	
		var old_url = document.getElementById(formId).action;
		//alert(old_url);
		old_url=old_url+"&operation="+oper;
		//alert(old_url);
		document.getElementById(formId).action = old_url;
		document.getElementById(formId).submit();
}

	
	
